<template>
  <div>
    <!--面包屑导航栏-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <router-link to="/home/goods">商品管理</router-link>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!--输入框区域-->
          <el-input placeholder="请输入内容" v-model="searchData" class="input-with-select">
            <el-button slot="append" @click="onSearch" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!--按钮区域-->
          <el-button type="primary" @click="goAdd">商品添加</el-button>
        </el-col>
      </el-row>
      <!--表格区域-->
      <el-table
          :data="ListData"
          style="width: 100%"
          stripe
          border
      >
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column property="goods_name" label="商品名称"></el-table-column>
        <el-table-column property="goods_price" label="商品价格（元）" width="95px"></el-table-column>
        <el-table-column property="goods_weight" label="商品重量" width="70px"></el-table-column>
        <el-table-column label="创建时间" width="160px">
          <template v-slot="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160px">
          <template v-slot="scope">
            <!--设置按钮-->
            <el-button type="primary" icon="el-icon-edit" @click="onSet(scope.row)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" @click="onDelete(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页器区域-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="AllData">
      </el-pagination>
    </el-card>
    <!--这里是编辑功能的对话框区域-->
    <el-dialog
        title="设置属性"
        :visible.sync="SetDialogVisible"
        width="50%"
        @close="onSetClose"
    >
      <el-form ref="SetForm" :model="SetForm" :rules="SetFormRules" label-width="80px" label-position="top">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="SetForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input type="number" v-model="SetForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input type="number" v-model="SetForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input type="number" v-model="SetForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="SetDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="onSetSend">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "List",
  data() {
    return {
      //发送请求的参数对象
      SendData:
          {
            query: '',
            pagenum: 1,
            pagesize: 10
          },
      //整个商品列表的数据
      ListData: [],
      //请求到的数据的总条数
      AllData: -1,
      //搜索框的内容数据用于改变请求参数里的查询参数
      searchData: '',
      //控制编辑对话框的显示和隐藏
      SetDialogVisible: false,
      //设置对话框里的表单数据源
      SetForm: {
        //商品名称
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
      },
      //设置对话框里的表单数据的验证规则
      SetFormRules: {
        goods_name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'}
        ],
        goods_price: [
          {required: true, message: '请输入商品价格', trigger: 'blur'}
        ],
        goods_number: [
          {required: true, message: '请输入商品数量', trigger: 'blur'}
        ],
        goods_weight: [
          {required: true, message: '请输入商品重量', trigger: 'blur'}
        ]
      },
      //被设置数据的id
      goods_id: 0

    }
  },
  methods: {
    //该方法用于获取整个商品列表的相关数据
    async getGoodsList() {
      const {data: res} = await this.$http.get('goods', {params: this.SendData});
      if (res.meta.status !== 200) return this.$message.error("获取商品列表数据失败");
      this.$message.success("获取商品列表数据成功")
      this.ListData = res.data.goods;
      this.AllData = res.data.total;
      console.log(this.ListData)
    },
    //列表页面展示数据大小变化触发的事件
    handleSizeChange(val) {
      this.SendData.pagesize = val;
      this.getGoodsList();
    },
    //列表页面页码变化是触发该函数
    handleCurrentChange(val) {
      this.SendData.pagenum = val;
      this.getGoodsList();
    },
    //点击搜索按钮时触发的函数
    onSearch() {
      this.SendData.query = this.searchData;
      this.getGoodsList();
    },
    //点击删除按钮的触发函数
    async onDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data: res} = await this.$http.delete(`goods/${id}`);
        console.log(res)
        if (res.meta.status !== 200) {
          this.$message.error('删除数据失败')
          return
        }
        await this.getGoodsList();
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //点击添加商品按钮的触发函数
    goAdd() {
      this.$router.push('add')
    },
    //点击设置按钮后的回调函数
    onSet(data) {
      const {goods_id, goods_name, goods_number, goods_price, goods_weight} = data;
      console.log(data)
      this.goods_id=goods_id;
      console.log(this.goods_id)
      this.SetForm.goods_number = goods_number;
      this.SetForm.goods_weight = goods_weight;
      this.SetForm.goods_price = goods_price;
      this.SetForm.goods_name = goods_name;
      this.SetDialogVisible = true;
    },
    //当设置功能的对话框关闭的时候触发
    onSetClose() {
      this.$refs.SetForm.resetFields();
    },
    //点击设置功能对话框里的确定按钮的回调函数
    async onSetSend() {
      console.log(this.goods_id)
      const {data: res} = await this.$http.put(`goods/${this.goods_id}`, this.SetForm);
      this.SetDialogVisible=false;
      console.log(res)
    }
  },
  created() {
    this.getGoodsList();
  }
}
</script>

<style scoped>

</style>
